package time_20230811;

/**
 * @author lizhen
 * @date 2023/8/11 16:46
 */

/**
 * 13.罗马数字转整数
 */
public class RomanToInt {
    public static void main(String[] args) {
        String s = "XLIX";
        int res = romanToInt(s);
        System.out.println(res);
    }

    public static int getInt(String s){
        switch (s){
            case "I":
                return 1;
            case "V":
                return 5;
            case "X":
                return 10;
            case "L":
                return 50;
            case "C":
                return 100;
            case "D":
                return 500;
            case "M":
                return 1000;
            case "a":
                return 4;
            case "b":
                return 9;
            case "c":
                return 40;
            case "d":
                return 90;
            case "e":
                return 400;
            case "f":
                return 900;

        }
        return 0;
    }

    public static String replaceString(String s){
        s = s.replace("IV", "a");
        s = s.replace("IX", "b");
        s = s.replace("XL", "c");
        s = s.replace("XC", "d");
        s = s.replace("CD", "e");
        s = s.replace("CM", "f");
        
        return s;
    }

    public static int romanToInt(String s){
        String roman = replaceString(s);
        
        int res = 0;
        for (int i = 0; i < roman.length(); i++) {
            res += getInt(String.valueOf(roman.charAt(i)));
        }

        return res;
    }
}
